/*
 * @Author: your name
 * @Date: 2021-11-18 10:59:08
 * @LastEditTime: 2021-12-03 16:01:56
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \go_work\src\xiaoshuo\models\commons\base.go
 */
package base

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/config"
	_ "github.com/go-sql-driver/mysql" // import your used driver
)

func init() {
	//读取数据库配置文件
	db_username, _ := config.String("db_username")
	db_password, _ := config.String("db_password")
	db_host, _ := config.String("db_host")
	db_port, _ := config.String("db_port")
	db_name, _ := config.String("db_name")
	db_charset, _ := config.String("db_charset")
	db_driver, _ := config.String("db_driver")

	// set default database
	orm.RegisterDriver(db_driver, orm.DRMySQL)
	/*
		参数一：数据库别名
		参数二：驱动名称
		参数三：数据库连接字符串:username:password@tcp(127.0.0.1:3306)/databasename?charset=utf8
	*/
	orm.RegisterDataBase("default", "mysql", db_username+":"+db_password+"@tcp("+db_host+":"+db_port+")/"+db_name+"?charset="+db_charset+"&loc=Local")

}
